package com.example.vvdemo.read;

import com.example.vvdemo.rw.entity.Candidate;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class Read {

    public void getByteArray(){
        //1. 连接Redis服务
        Jedis jedis = new Jedis("192.168.199.109",6379);

        final List<Candidate> candidateList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            //------------------------------------------------
            //2.1 准备key
            String key = "candidate";
            //2.2 将key转换为byte[]
            byte[] byteKey = SerializationUtils.serialize(key);
            //2.3 jedis去Redis中获取value
            byte[] value = jedis.get(byteKey);
            //2.4 将value反序列化为User对象
            final Candidate candidate = (Candidate) SerializationUtils.deserialize(value);
            //2.5 输出
            System.out.println("candidate:" + candidate);
            candidateList.add(candidate);

            //------------------------------------------------
        }

        //3. 释放资源
        jedis.close();
    }
}
